<template>
  <div>
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>

<script>
import UploadExcel from '@/components/UploadExcel/index.vue'
import { formatExcelDate } from '@/utils/index.js'
import { addBatchEmployee } from '@/api/employees'
export default {
  components: {
    UploadExcel
  },
  methods: {
    async handleSuccess(data) {
      console.log(data, 111111)
      const { results } = data
      const list = this.formatExcel(results)
      console.log(list)
      await addBatchEmployee(list)
      this.$router.push('/employees')
    },
    formatExcel(results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      //   console.log(results)
      return results.map(obj => {
        const newObj = {}
        const zhName = Object.keys(obj)
        zhName.forEach(item => {
          const enName = mapInfo[item]
          if (enName === 'timeOfEntry' || enName === 'correctionTime') {
            newObj[enName] = new Date(formatExcelDate(obj[item]))
          } else {
            newObj[enName] = obj[item]
          }
        })
        return newObj
      })
    }
  }
}
</script>

<style>

</style>
